package com.selvn.encryptnote;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;

public class EncryptNote extends Activity {
    //private static final String password = "Password"; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // if no password is set, directly jump to note list.
        if(GetPassword().equals(""))
        {
            finish();
            // Jump to the note
            Intent i = new Intent(this, NoteList.class);
            startActivity(i);
        }
        else
        {
        	// show the input password panel
            setContentView(R.layout.login);
            
            EditText passET = (EditText) findViewById(R.id.Pass);
            passET.setTransformationMethod(new PasswordTransformationMethod());
        }
    }
        
	public void LoginButtonClick( View v )
	{
		EditText passET = (EditText) findViewById(R.id.Pass);
		String passString = passET.getText().toString();
		switch (v.getId() )
		{
		case R.id.Button00:
			passET.setText(passString+="0");
			break;
		case R.id.Button01:
			passET.setText(passString+="1");
			break;
		case R.id.Button02:
			passET.setText(passString+="2");
			break;
		case R.id.Button03:
			passET.setText(passString+="3");
			break;
		case R.id.Button04:
			passET.setText(passString+="4");
			break;
		case R.id.Button05:
			passET.setText(passString+="5");
			break;
		case R.id.Button06:
			passET.setText(passString+="6");
			break;
		case R.id.Button07:
			passET.setText(passString+="7");
			break;
		case R.id.Button08:
			passET.setText(passString+="8");
			break;
		case R.id.Button09:
			passET.setText(passString+="9");
			break;
			/*
		case R.id.Reset:
			passET.setText("");
			break;
			*/
		case R.id.Confirm:
		    //Func.Toast(this, GetPassword());
		    passET.setText("");
			if( passString.equals(GetPassword()))
			{
				// quit the login panel 
				finish();
				// Jump to the note
				Intent i = new Intent(this, NoteList.class);
		        startActivity(i);
			}
			else
			{
				Func.MessageBox(this, R.string.wrongPassword, R.string.wrongPasswordTitle, R.string.confirm); 
			}
			break;
		}
	}
	private String GetPassword()
	{
    	SharedPreferences settings = getSharedPreferences(Config.SETTINGS,Activity.MODE_PRIVATE);    
        String value = settings.getString(Config.SETTINGS, "");
        return value;
	}
}
